package com.example.databasehomework;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Activity_AddStudentsByXls extends AppCompatActivity {

    Button addStudents, addCourseChoosen;

    protected String[] needPermissions = {
            Manifest.permission.ACCESS_COARSE_LOCATION,//定位权限
            Manifest.permission.ACCESS_FINE_LOCATION,//定位权限
            Manifest.permission.WRITE_EXTERNAL_STORAGE,//存储卡写入权限
            Manifest.permission.READ_EXTERNAL_STORAGE,//存储卡读取权限
            Manifest.permission.READ_PHONE_STATE//读取手机状态权限
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_students_by_xls);
        ActivityCompat.requestPermissions(this, needPermissions, 0);
        getViews();
        setListeners();
    }

    public void getViews() {
        addStudents = findViewById(R.id.addStudents);
        addCourseChoosen = findViewById(R.id.addCourseChoosen);
    }

    public void setListeners() {
        addStudents.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = getIntent();
                Uri uri = intent.getData();
                try {
                    InputStream is = getContentResolver().openInputStream(uri);
                    File cache = new File(Activity_AddStudentsByXls.this.getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath(), "hbhdfjy.xls");
                    FileOutputStream fos = new FileOutputStream(cache);
                    FileUtils.copy(is, fos);
                    fos.close();
                    is.close();
                    addStudents(cache, 0);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
        addCourseChoosen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = getIntent();
                Uri uri = intent.getData();
                try {
                    InputStream is = getContentResolver().openInputStream(uri);
                    File cache = new File(Activity_AddStudentsByXls.this.getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath(), "hbhdfjy.xls");
                    FileOutputStream fos = new FileOutputStream(cache);
                    FileUtils.copy(is, fos);
                    fos.close();
                    is.close();
                    addStudents(cache, 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    public void addStudents(File file, int status) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean success = HttpUnit.addStudentByXls(file, status);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (success) {
                            Toast.makeText(getBaseContext(), "导入学生信息成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getBaseContext(), "导入学生信息失败", Toast.LENGTH_SHORT).show();}
                    }
                });
            }
        }).start();
    }
}